import { msg } from './util.js'

// 基础URL

// let BASE_URL = 'http://localhost:8082'

let BASE_URL = 'http://10.170.81.181:8082'
// let BASE_URL = 'http://192.168.155.235:8083'
// let BASE_URL = 'http://150.158.157.211:8082'
// let BASE_URL = 'http://leohc.vip:8082'


import store from '@/store'

const request = (options = {}) => {
	
	//判断是否有访问令牌
	const accessToken = store.state.accessToken
	if(accessToken){
		options.header = {'Authorization':`Bearer ${accessToken}`}
	}
	// resolve 正常响应，reject异常响应
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: options.data || {},
			timeout: 8000, // 8秒超时时间，单位ms
			header:options.header || {},
			success: (res) => {
				// console.log('res', res.data)
				resolve(res.data)
			},
			fail: (err) => {
				// console.log('err', err)
				msg('请求接口失败')
				
				reject(err)
			}
		})
	}) 
}

// 导出
export default request